﻿using Aspose.Hook;
using Aspose.Words.Reporting;
using Aspose.Words.Tables;
using Aspose.Words;
using NUnit.Framework;
using System.Drawing;
using System.Text.RegularExpressions;
using Aspose.Words.Replacing;

namespace WordStudy20250123.T001
{
    [TestFixture]
    public class WordTest02
    {

        [SetUp]
        public void Setup()
        {
            HookManager.ShowHookDetails(true);
            HookManager.StartHook();
        }

        [Test]
        public void Test01()
        {
            string templatePath = @"D:\data\WordStudy20250123\WordTest02\template.docx";
            GenerateTemplate(templatePath);
        }

        [Test]
        public void Test02()
        {
            string templatePath = @"D:\data\WordStudy20250123\WordTest02\template.docx";
            string reportPath = @"D:\data\WordStudy20250123\WordTest02\report.docx";
            GenrateReport(templatePath, reportPath);
        }

        public static void CreateSimpleTable(string outputPath)
        {
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);

            // Start building the table.
            builder.StartTable();
            builder.InsertCell();
            builder.Write("Row 1, Cell 1 Content.");

            // Build the second cell.
            builder.InsertCell();
            builder.Write("Row 1, Cell 2 Content.");

            // Call the following method to end the row and start a new row.
            builder.EndRow();

            // Build the first cell of the second row.
            builder.InsertCell();
            builder.Write("Row 2, Cell 1 Content");

            // Build the second cell.
            builder.InsertCell();
            builder.Write("Row 2, Cell 2 Content.");
            builder.EndRow();

            // Signal that we have finished building the table.
            builder.EndTable();
            doc.Save(outputPath);
            Console.WriteLine("文档已生成");
        }

        public static void GenerateTemplate(string outputPath)
        {
            try
            {
                // 创建新文档
                var doc = new Document();
                var builder = new DocumentBuilder(doc);

                // 设置默认字体
                builder.Font.Name = "宋体";
                builder.Font.Size = 16;
                builder.Font.Bold = true;

                // 添加标题
                builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
                builder.Writeln("学生成绩报告");
                builder.Writeln();

                // 重置字体
                builder.Font.Size = 12;
                builder.Font.Bold = false;
                builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;

                // 添加基本信息
                builder.Writeln("标题：<<[Title]>>");
                builder.Writeln("日期：<<[Date]>>");
                builder.Writeln();

                // 添加图片占位符
                builder.Writeln("学校标志：");
                builder.Writeln();
                builder.InsertImage(@"D:\data\WordStudy20250123\WordTest02\dummy.png", 80, 80);  // 插入一个占位图片
                builder.Writeln();
                builder.Writeln("<<image [PhotoPath]>>");
                //builder.Writeln("<<image [PhotoPath]  scale(100, 100)>>");

                // 添加总体评价
                builder.Writeln("总体评价：");
                //builder.Writeln("<<if [IsPass]>>");
                //builder.Writeln(" <<[\"合格\" tc \"000000\"]>>");
                //builder.Writeln("<<else>>");
                //builder.Writeln("<<[\"不合格\" tc \"FF0000\"]>>");
                //builder.Writeln("<<endif>>");
                //builder.Writeln();

                // 添加表格标题
                builder.Writeln("学生成绩表：");
                builder.Writeln(); // 添加空行
                builder.Write("\u2611");

                // 开始创建表格
                Table table = builder.StartTable();
                // 添加表头行
                builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
                builder.Font.Bold = true;
                builder.CellFormat.Width = 100; // 设置单元格宽度

                // 插入表头
                builder.InsertCell();
                builder.Write("姓名");
                builder.InsertCell();
                builder.Write("成绩");
                builder.InsertCell();
                builder.Write("评价");
                builder.EndRow();

                // 重置字体粗细
                builder.Font.Bold = false;

                // 插入数据行（使用LINQ标记）
                builder.InsertCell();
                builder.Write("<<foreach [row in Students]>><<[row.Name]>>");
                builder.InsertCell();
                builder.Write("<<[row.Score]>>");
                builder.InsertCell();
                //builder.Write("<<if [row.IsQualified]>>合格");
                //builder.Write("<<else>>");
                //builder.Write("不合格<</if>><</foreach>>");
                //builder.EndRow();


                // Wingdings字体中，'o'是空方框，'ü'是打钩的方框
                //                ☐ (\u2610) -空方框
                //☑ (\u2611) -打钩的方框
                //☒ (\u2612) -打X的方框
                //□ (\u25A1) -另一种空方框
                //■ (\u25A0) -实心方框
                //✓ (\u2713) -对勾
                //✗ (\u2717) -X号
                builder.Font.Name = "Wingdings";

                //builder.Write("ü"); // 打钩



                builder.Font.Name = "宋体"; // 切回宋体
                builder.Write(" <<if [row.IsQualified]>><<textColor [\"black\"]>>\u2611合格<</textColor>>");
                builder.Write("<<else>>");
                builder.Font.Name = "Wingdings";
                //builder.Write("o"); // 空方框
                builder.Font.Name = "宋体"; // 切回宋体
                builder.Write("<<textColor [\"red\"]>>\u25A1不合格<</textColor>><</if>><</foreach>>");

                builder.EndRow();

                // 结束表格
                builder.EndTable();

                // 在表格前后添加LINQ foreach标记
                //var range = doc.Range;
                //range.Replace("学生成绩表：", "学生成绩表：\n<<foreach [row in Students]>>");
                //// 在表格后添加endforeach
                //builder.Writeln("<</foreach>>");

                // 设置表格边框和样式

                table.SetBorders(LineStyle.Single, 1.0, Color.Black);

                // 设置表格样式
                foreach (Row row in table.Rows)
                {
                    foreach (Cell cell in row.Cells)
                    {
                        // 设置单元格格式
                        cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
                        cell.CellFormat.Width = 100;
                    }
                }

                // 添加注意事项
                builder.Writeln();
                builder.Writeln();
                builder.Font.Size = 10.5;
                builder.Font.Color = Color.Gray;
                builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
                builder.Writeln("注：");
                builder.Writeln("1. 成绩大于等于60分为合格");
                builder.Writeln("2. 本报告自动生成，无需签字");

                // 保存模板
                doc.Save(outputPath);
                Console.WriteLine("文档已生成");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"生成模板时出错: {ex.Message}\n\n{ex.StackTrace}");
            }
        }

        public static void GenrateReport(string templatePath, string outputPath)
        {
            try
            {
                // 加载模板文档
                Document doc = new Document(templatePath);                

                // 准备数据
                var data = new ReportData
                {
                    Title = "学生成绩报告",
                    Date = DateTime.Now.ToString("yyyy-MM-dd"),
                    PhotoPath = @"D:\data\WordStudy20250123\WordTest02\dingliren.png",
                    IsPass = true,
                    Students = new List<Student>
                    {
                        new Student
                        {
                            Name = "zhangsan",
                            IsQualified = true,
                            Score = 85,
                        },
                        new Student
                        {
                            Name = "lisi",
                            IsQualified = false,
                            Score = 59,
                        },
                        new Student
                        {
                            Name = "wangwu",
                            IsQualified = true,
                            Score = 92,
                        },
                    },
                };

                // 创建引擎并配置
                var engine = new ReportingEngine();

                // 使用简单的数据源名称
                engine.BuildReport(doc, data, "report");

                string PhotoPath = @"D:\data\WordStudy20250123\WordTest02\dingliren.png";
                FindReplaceOptions options = new FindReplaceOptions
                {
                    ReplacingCallback = new ImageReplaceCallback(PhotoPath, 50, 20)
                };
                doc.Range.Replace(new Regex(@"\$PhotoPath\$"), "", options);

                // 保存文档
                doc.Save(outputPath);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"生成报告时出错: {ex.Message}\n\n{ex.StackTrace}");
            }
        }


        public class Student
        {
            public string Name { get; set; }
            public bool IsQualified { get; set; }
            public double Score { get; set; }
        }

        public class Course
        {
            public string CourseName { get; set; }
            public double Credit { get; set; }
        }

        public class ReportData
        {
            public string Title { get; set; }
            public string Date { get; set; }
            public string PhotoPath { get; set; }
            public bool IsPass { get; set; }
            public List<Student> Students { get; set; }
            public List<Course> Courses { get; set; }
        }
    }

    
}
